package com.iyoyi.skinlib.widget;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.util.TypedValue;

import androidx.annotation.NonNull;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.content.ContextCompat;

public class TextViewFactory extends AbstractViewFactory<AppCompatTextView> {
    private static TextViewFactory sInst;

    private TextViewFactory() {/**/}

    public static TextViewFactory get() {
        synchronized (TextViewFactory.class) {
            if (sInst == null) {
                sInst = new TextViewFactory();
            }
        }
        return sInst;
    }

    @NonNull
    public static AppCompatTextView createTextView(Context context, AttributeSet attrs) {
        return new AppCompatTextView(context, attrs);
    }

    @Override
    void onAttributeUpdate(AppCompatTextView view, Resources resources, String name, int id) {
        switch (name) {
            case "text":
                view.setText(resources.getString(id));
                break;

            case "textSize":
                view.setTextSize(TypedValue.COMPLEX_UNIT_PX, resources.getDimension(id));
                break;

            case "textColor":
                view.setTextColor(ContextCompat.getColor(view.getContext(), id));
                break;

            case "background":
                view.setBackground(AppCompatResources.getDrawable(view.getContext(), id));
                break;
        }
    }
}
